// pages/list/list.js
const db = wx.cloud.database()
const memo = db.collection('memocyou')
const _ = db.command
Page({

  /**
   * 页面的初始数据
   */
  data: {
    loading: true,
    pagenum:0,
    show: false,
    delid:"",
    tap:0,

    option1: [
      { text: '全部', value: 0 },
      { text: '随记', value: 1 },
      { text: '任务', value: 2 },
    ],
    option2: [
      { text: '默认排序', value: 'a' },
      { text: '时间倒序', value: 'b' },
    ],
    value1: 0,
    value2:'a'
  },
  getdata(){
    if(this.data.value1==0){
      if(this.data.value2=='a'){
        memo .orderBy('createdate', 'desc').get().then(res=>{
          this.setData({
            list:res.data.map(v=>({...v,createdate:(new Date(v.createdate).toLocaleString())})),
          })
        })
      }else{
        memo .orderBy('createdate', 'asc').get().then(res=>{
          this.setData({
            list:res.data.map(v=>({...v,createdate:(new Date(v.createdate).toLocaleString())})),
          })
        })
      }
      
    }else if(this.data.value1==1){
      if(this.data.value2=='a'){
        memo .orderBy('createdate', 'desc').where({border:'1'}).get().then(res=>{
          this.setData({
            list:res.data.map(v=>({...v,createdate:(new Date(v.createdate).toLocaleString())})),
          })
        })
      }else{
        memo .orderBy('createdate', 'asc').where({border:'1'}).get().then(res=>{
          this.setData({
            list:res.data.map(v=>({...v,createdate:(new Date(v.createdate).toLocaleString())})),
          })
        })
      }
      
    }else if(this.data.value1==2){
      if(this.data.value2=='a'){
        memo .orderBy('createdate', 'desc').where({border:'2'}).get().then(res=>{
          this.setData({
            list:res.data.map(v=>({...v,createdate:(new Date(v.createdate).toLocaleString())})),
          })
        })
      }else{
        memo .orderBy('createdate', 'asc').where({border:'2'}).get().then(res=>{
          this.setData({
            list:res.data.map(v=>({...v,createdate:(new Date(v.createdate).toLocaleString())})),
          })
        })
      }
     
    }
  },
  changeitem(res){
    console.log(res);
    this.setData({
      value1:res.detail
    });
    this.getdata();
  },
  changeitem2(res){
    console.log(res);
    this.setData({
      value2:res.detail
    });
    this.getdata();
  },

  del(res){
    console.log('这次删除的是'+this.data.delid); 
    memo.doc(this.data.delid).field({
      imgfileID:true,
    }).get().then(res=>{console.log(res);
      console.log(res.data.imgfileID);
      this.delcloudimg(res.data.imgfileID.map(v=>v.fileID));
    });
    memo.doc(this.data.delid).remove().then(res=>{console.log(res)});
    this.onClose();
  },
    //云端删除图片
  delcloudimg(event){
    wx.cloud.deleteFile({
      fileList:event,
      success: res => {
        // handle success
        console.log(res.fileList)
      },
      fail: console.error
    })
  },

  showPopup(res) {
    const delid=res.currentTarget.dataset.id
    this.setData({ show: true,delid,tap:1 });
    console.log(res);
  },
  onClose() {
    this.setData({ show: false,tap:0 });
  },
  addclick(){
    wx.navigateTo({
      url: '../add/add',
    })
  },
  click(res){
    if(this.data.tap==0){
      let id=res.currentTarget.dataset.id
      wx.navigateTo({
        url: '../updata/updata?id='+id,
      })
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    memo .orderBy('createdate', 'desc').get().then(res=>{
      this.setData({
        list:res.data.map(v=>({...v,createdate:(new Date(v.createdate).toLocaleString())})),
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.setData({
      loading: false,
    });
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getdata();
    console.log('显示list')
    this.getTabBar().init();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    memo.orderBy('createdate','desc').get().then(res=>{
      this.setData({ list:res.data.map(v=>({...v,createdate:(new Date(v.createdate).toLocaleString())})),});
      console.log("数据已更新");
      this.data.pagenum=0;
      wx.stopPullDownRefresh()
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
    
    let pagenum=this.data.pagenum+20;
    memo.skip(pagenum).get().then(res=>{
      let new_data=res.data.map(v=>({...v,createdate:(new Date(v.createdate).toLocaleString())}));
      let old_data=this.data.list;
      this.setData({ 
        old_data:old_data.map(v=>({...v,createdate:(new Date(v.createdate).toLocaleString())})),
        list:old_data.concat(new_data),
       
        pagenum:pagenum
      });
      console.log("数据已更新");
      console.log("触底")
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})